<?php
   
     define('ACC', true);
require_once('../include/init.php');
      

     $getData = $_POST;
     $data = array();
     $goods = new goodModel();

     //自动过滤
     $data =  $goods->shiftData($getData);
     $num = $goods->autoNum();

     if($num){
        $data['goods_sn'] = $num;
     }

// 验证数据
    $re = $goods->_validate($data);
   
     $err = $goods->getErr();

     if($re){
        echo '验证成功';
     }else{
        echo implode(',',$err);
        exit;
     }


     //图片
       $file = new UpTool();
       $url = $file->up('ori_img');
       if(!$url){
          print_r($file->error());
       }
       $data['ori_img'] = $url;


  // 将略缩图加入数据库
 // 如果$ori_img上传成功，再次生成中等大小缩略图 300*400 goods_aa.jpeg
 // 和浏览时用的缩略图 160*220 thumb_aa.jpeg
if($url){
    //给缩略图重新生成名字 , 并把路径改为绝对路径
    $goodsdir = ROOT.substr($url,0,strrpos($url,"/")+1) . 'goods_'.substr($url,strrpos($url,"/")+1);
    $thumbdir = ROOT.substr($url, 0,strrpos($url,"/")+1).'thumb_'.substr($url, strrpos($url, "/")+1);
    //调用图片处理类生成缩略图
    if(ImageTool::thumb(ROOT . $url,300,400,$goodsdir)){
        $data['goods_img'] = str_replace(ROOT,'',$goodsdir);
    }
    if(ImageTool::thumb(ROOT . $url,160,220,$thumbdir)){
        $data['thumb_img'] = str_replace(ROOT,'',$thumbdir);
    }
}

     if($goods_id = $goods->add($data)){
           echo "商品发布成功";
     }else{
           echo "商品发布失败";
     }

    
    // var_dump();


?>